package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.dao.index.WayBillIndexRepository;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototyoe")
public class WayBillAction extends BaseAction<WayBill> {
    //打印日志信息
    private static final Logger LOGGER = Logger.getLogger(WayBillAction.class);
    @Autowired
    private WayBillService wayBillService;
    @Autowired
    private WayBillIndexRepository wayBillIndexRepository;

    //waybill_save.action,新添加的运单数据保存到数据表中waybill_save.action
    @Action(value = "waybill_save",results = {@Result(name = "success",type = "json")})
    public String save(){

        WayBill wayBillNum = wayBillService.findByWayBillNum(model.getWayBillNum());

        if (wayBillNum == null){
            wayBillService.save(model);
            wayBillIndexRepository.save(model); //运单保存到索引库

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("success",true);
            hashMap.put("msg","保存运单成功");
            LOGGER.info("运单保存成功，运单号："+ model.getWayBillNum());
            ActionContext.getContext().getValueStack().push(hashMap);

        }else try {
            //将查询出来的运单号主键赋值给model
            model.setId(wayBillNum.getId());
            //使用工具类将后者属性复制到前者
            BeanUtils.copyProperties(wayBillNum, model);
            wayBillService.save(wayBillNum);    //保存运单导数据库

            wayBillIndexRepository.save(wayBillNum);    //运单保存到索引库

            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("success",true);
            hashMap.put("msg","保存运单成功");
            LOGGER.info("运单保存成功，运单号："+ model.getWayBillNum());
            ActionContext.getContext().getValueStack().push(hashMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("success",false);
            hashMap.put("msg","保存运单失败....");
            LOGGER.info("保存运单失败，运单号："+ model.getWayBillNum());
            ActionContext.getContext().getValueStack().push(hashMap);
            e.printStackTrace();
        }
        return SUCCESS;
    }


    //添加 pageQuery 的方法waybill_pageQuary.action
    @Action(value = "waybill_pageQuary",results = {@Result(name = "success",type = "json")})
    public String pageQuery(){

        PageRequest request = new PageRequest(page-1,rows, Sort.Direction.ASC,"id");
        Page<WayBill> page = wayBillService.findPageData(model,request);
        pushPageDataToValueStrack(page);

        return SUCCESS;
    }

    // waybill_findByWayBillNum异步请求根据运单号查询订单

    @Action(value = "waybill_findByWayBillNum",results = {@Result(name = "success",type = "json")})
    public String findByWayBillNum(){
         WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
        HashMap<String, Object> hashMap = new HashMap<>();
        if (wayBill==null){
            //没有运单号
            hashMap.put("success",false);
        }else{
            hashMap.put("success",true);
            hashMap.put("orderData",wayBill);
        }
        ActionContext.getContext().getValueStack().push(hashMap);


        return SUCCESS;
    }




}
